package com.visikard.ui.fragment;
/**
 * effect fragment use on kard builder
 */
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.visikard.application.Constant;
import com.visikard.ui.R;
import com.visikard.ui.activity.CouponBuilderActivity;
import com.visikard.utility.BitmapUtils;
import com.visikard.utility.UILImageUtils;
import com.visikard.utility.Utils;
import com.visikard.widget.SpanZoomImageView25;

public class CBEffectFragment extends BaseFragment implements ViewClickable {
	
	private static final int REQUEST_CODE_PICK_LIBRARY = 0;
	/*
	 * camera for kbuilder or camera for post photo
	 */
	public static final String EXT_DATA_URI = "KBKardEffect24Fragment_ext_data_uri";	
	
	public static final int VIEW_TYPE_KBUILDER = 0;
	public static final int VIEW_TYPE_POST_PHOTO = 1;	
	
	private Uri imageUri;
	private SpanZoomImageView25 kardImage;
//	private ImageView mRotateButton;
	
	private int mBitmapWidth, mBitmapHeight;
		
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getActivity().getWindow().getDecorView().findViewById(android.R.id.content).setVisibility(View.VISIBLE);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {	
		View v = inflater.inflate(R.layout.cb_effect, container, false);
		return v;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {		
		super.onActivityCreated(savedInstanceState);		
		
		Bundle bundle = getArguments();
		if( bundle != null ) {
			imageUri = Uri.parse(bundle.getString((EXT_DATA_URI)));
		}						
        
		kardImage = (SpanZoomImageView25) getView().findViewById(R.id.kard_image);
		
		if(CouponBuilderActivity.couponBitmap != null){
			setPreviewBitmap(CouponBuilderActivity.couponBitmap);
		}
		else if( imageUri != null ) {
			if(imageUri.getScheme().contains("http")) {
//				ImageLoader.getInstance().loadImage(kardImage, imageUri.toString(), Device.getDeviceWidth(), BitmapUtils.ROUND_NONE, 0, 0, 0);
				UILImageUtils.loadImage(kardImage, imageUri.toString(), 0, BitmapUtils.ROUND_NONE, 0, 0, 0, 0, 0);
			}
		}
		
		kardImage.setEnableEdit(false);					
					
	}		
	
	/**
	 * resize input bitmap and set to preview frame
	 * also set original bitmap to KardBuiderActivity variable
	 * @param bitmap
	 */
	private void setPreviewBitmap(Bitmap bitmap) {
		if(bitmap == null) {
			Utils.showToast(getActivity(), R.string.error_message);
			return;
		}
		bitmap = getResizeBitmap(bitmap);
		kardImage.setImageBitmap(bitmap);
	}
	
	/**
	 * resize input bitmap to save memory
	 * @return
	 */
	private Bitmap getResizeBitmap(Bitmap bitmap) {
		mBitmapWidth = bitmap.getWidth();
		mBitmapHeight = bitmap.getHeight();
		
		if(mBitmapWidth > Constant.BITMAP_MAX_SIZE || mBitmapHeight > Constant.BITMAP_MAX_SIZE) {
			if(mBitmapWidth > mBitmapHeight) {
				// scale base on width
				mBitmapHeight = mBitmapHeight * Constant.BITMAP_MAX_SIZE / mBitmapWidth;
				mBitmapWidth = Constant.BITMAP_MAX_SIZE;			
			} else {
				// scale base on height
				mBitmapWidth = mBitmapWidth * Constant.BITMAP_MAX_SIZE / mBitmapHeight;
				mBitmapHeight = Constant.BITMAP_MAX_SIZE;
			}
			bitmap = Bitmap.createScaledBitmap(bitmap, mBitmapWidth, mBitmapHeight, true);
		}
		
		return bitmap;
	}
	
	private void getFromLibrary() {
		Intent intentPhotoLibrary = new Intent(Intent.ACTION_PICK,
				android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
		intentPhotoLibrary.setType("image/*");
		startActivityForResult(intentPhotoLibrary, REQUEST_CODE_PICK_LIBRARY);
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if( resultCode == Activity.RESULT_OK ) {
			switch ( requestCode ) {
			case REQUEST_CODE_PICK_LIBRARY:
				try {															
					Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), data.getData());
					if(bitmap != null){
						setPreviewBitmap(bitmap);
						CouponBuilderActivity.couponBitmap = bitmap;
					}
				} catch (Exception e) {
					
				}	   
				break;
			default:
				break;
			}
		}			
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public void onViewClick(View v) {
		switch (v.getId()) {
		case R.id.camera_next:
			((CouponBuilderActivity)getActivity()).nextFragment();
			break;
		case R.id.camera_back:
			((CouponBuilderActivity)getActivity()).onBackPressed();
			break;
		case R.id.camera_capture:
			((CouponBuilderActivity)getActivity()).onBackPressed();
			break;
		case R.id.camera_library:
			getFromLibrary();
			break;
//		case R.id.camera_rotate:
//			if(CouponBuilderActivity.couponBitmap != null) {
//				setPreviewBitmap(BitmapUtils.rotate(CouponBuilderActivity.couponBitmap, 90));
//			}
//			break;
		default:
			break;
		}		
	}
	
}
